#include "changecode.h"
#include "ui_changecode.h"
#include "utils.h"
#include <QMessageBox>

using namespace utils;

changeCode::changeCode(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::changeCode) {
    ui->setupUi(this);
}

changeCode::~changeCode() {
    delete ui;
}

void changeCode::getData(Student *stu) {
    this->stu = stu;
}

void changeCode::on_pushButton_clicked() {
    QString str = ui->lineEdit->text();
    string newcode = string((const char *)str.toLocal8Bit());
    if(newcode.size() == 0) {
        QMessageBox::warning(this, "warning", "请输入密码");
        return;
    } else if(newcode.length() < 6) {
        QMessageBox::warning(this, "warning", "密码长度不得小于6字节");
        return;
    } else if(newcode.length() > 16) {
        QMessageBox::warning(this, "warning", "密码长度不得大于16字节");
        return;
    } else if(!strcmp(stu->pwdHash, (sha1(sha1(newcode.data()) + (string)(stu->pwdSalt))).data())) {
        QMessageBox::warning(this, "warning", "新密码不得与旧密码相同");
        return;
    }
    strcpy(stu->pwdHash, (sha1(sha1(newcode.data()) + (string)(stu->pwdSalt))).data());
    rewrite(stu->ID, stu, studentDatPath);
    addLog("[changePwd] status 0: userID[" + to_string(stu->ID) + "] change password successfully");
    QMessageBox::warning(this, "提示", "修改成功！");
    this->close();
}
